package com.franklin.ideaplugin.maphelper.completion;

import cn.hutool.core.collection.CollectionUtil;
import com.franklin.ideaplugin.api.utils.psi.PsiClassUtils;
import com.franklin.ideaplugin.maphelper.beans.colmember.IColMember;
import com.franklin.ideaplugin.maphelper.beans.colmember.PsiColMember;
import com.franklin.ideaplugin.maphelper.completion.result.MapLookupElementFactory;
import com.franklin.ideaplugin.maphelper.col.declaration.IColDeclaration;
import com.franklin.ideaplugin.maphelper.resolver.impl.map.MapMemberResolverFactory;
import com.franklin.ideaplugin.maphelper.utils.PsiMapUtils;
import com.google.common.collect.Sets;
import com.intellij.codeInsight.completion.CompletionContributor;
import com.intellij.codeInsight.completion.CompletionParameters;
import com.intellij.codeInsight.completion.CompletionResultSet;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author Ye Junhui
 * @since 2024/3/4
 */
public class MapDataCompletionContributor extends CompletionContributor {

    @Override
    public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
        //当前PSI元素
        PsiElement psiElement = parameters.getPosition();
        //表达式
        PsiExpression psiExpression = PsiMapUtils.getMapExpression(psiElement);
        if (Objects.isNull(psiExpression)){
            return;
        }
        ApplicationManager.getApplication().invokeLater(() -> {
            try {
                //解析成员
                Collection<IColMember> colMembers = PsiMapUtils.resolveMapMembers(psiExpression);

                //输出
                List<LookupElement> lookupElementList = MapLookupElementFactory.buildLookupElement(psiExpression, colMembers);
                result.addAllElements(lookupElementList);
            } finally {

            }
        });
    }

}
